# 定义时间变量名和显示时间格式
CURRENT_DATE=$(date +%Y%m%d-%H%M%S)
PP_NAME=open-ehr-ui
GIT_PATH=/openehr/deploy/source_code/open-ehr
PROJECT_PATH=${GIT_PATH}/2.code/open-ehr-manage/open-ehr-manage-ui
PROJECT_JAR_PATH=${PROJECT_PATH}/
DEV_DEPLOY_PATH=/openehr/docker/nginx/html
#删除项目目录原来的dist目录
rm -rf ${PROJECT_JAR_PATH}/dist/
# 判断目录是否存在
if [ ! -d "${DEV_DEPLOY_PATH}" ]; then
mkdir -p ${DEV_DEPLOY_PATH}
fi
# 进入GIT仓库目录
cd ${GIT_PATH}
# 检测代码是否有更新
echo "拉取最新版本"
LOG="PULL"
for ((i=0;i<=3;i++))
do
 LOG=$(git pull)
 if [ "${LOG}" = "Already up-to-date." ]; then
     echo "更新到最新版本，开始编译"
     break
 fi
done
if [ "${LOG}" = "Already up-to-date." ]; then
  # 开始打包程序
  cd ${PROJECT_PATH}
  npm run build:prod
  if [ $? != 0 ]
  then
    echo "打包时出现错误"
    exit 1
  fi
fi

# 删除历史dist
cd /openehr/docker/nginx/html
#暂时不备份
#cp ${DEV_DEPLOY_PATH}/dist -R ${DEV_DEPLOY_PATH}/dist.${CURRENT_DATE}.bak
rm -rf dist
echo "开始将dist拷贝到${DEV_DEPLOY_PATH}"
cp  ${PROJECT_JAR_PATH}/dist/ -R ${DEV_DEPLOY_PATH}
echo "部署完成"
